package com.charm.bulkcomponent

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentTransaction
import android.view.animation.AccelerateInterpolator
import android.view.animation.Animation
import android.widget.RelativeLayout
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.ashokvarma.bottomnavigation.BottomNavigationBar
import com.ashokvarma.bottomnavigation.BottomNavigationItem
import com.charm.frameworkarou.animation.Rotate3dAnimation
import kotlinx.android.synthetic.main.activity_main.*
import me.yokeyword.fragmentation.SupportActivity


@Route(path = "/common/main")
class MainActivity : SupportActivity() {
    private var fragments: ArrayList<Fragment> = ArrayList()
    private var fm: FragmentManager? = null
    private var transaction: FragmentTransaction? = null
    private var fragmentTags = arrayOf("home", "lore", "transaction", "circle", "workBench")
    private lateinit var homeBottomItem: BottomNavigationItem
    private lateinit var loreBottomItem: BottomNavigationItem
    private lateinit var circleBottomItem: BottomNavigationItem
    private lateinit var workBenchBottomItem: BottomNavigationItem

    private var currentFragment: Fragment? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
        initFragment()
        initListener()
    }


    private fun initListener() {
        bottom_view.setTabSelectedListener(object : BottomNavigationBar.OnTabSelectedListener {
            override fun onTabReselected(position: Int) {
                selectTab(position)
            }

            override fun onTabUnselected(position: Int) {

            }

            override fun onTabSelected(position: Int) {
                selectTab(position)
            }
        })
    }

    private fun initView() {
        homeBottomItem = BottomNavigationItem(R.mipmap.ic_launcher, R.string.bottom_home).setActiveColorResource(R.color.colorAccent)
        loreBottomItem = BottomNavigationItem(R.mipmap.ic_launcher, R.string.bottom_second).setActiveColorResource(R.color.colorAccent)
        circleBottomItem = BottomNavigationItem(R.mipmap.ic_launcher, R.string.bottom_third).setActiveColorResource(R.color.colorAccent)
        workBenchBottomItem = BottomNavigationItem(R.mipmap.ic_launcher, R.string.bottom_fourth).setActiveColorResource(R.color.colorAccent)
        bottom_view
                .addItem(homeBottomItem)
                .addItem(loreBottomItem)
                .addItem(circleBottomItem)
                .addItem(workBenchBottomItem)
                .setFirstSelectedPosition(0)
                .initialise()
    }

    private fun initFragment() {
        fm = supportFragmentManager
        transaction = fm!!.beginTransaction()
        fragments.add(ARouter.getInstance().build("/watercress/main").navigation() as Fragment)
        fragments.add(ARouter.getInstance().build("/quicktrade/main").navigation() as Fragment)
        fragments.add(Fragment())
        fragments.add(ARouter.getInstance().build("/common/userCenter").navigation() as Fragment)
        transaction!!
                .add(R.id.fragment_container, fragments[0], fragmentTags[0])
                .add(R.id.fragment_container, fragments[1], fragmentTags[1])
                .add(R.id.fragment_container, fragments[2], fragmentTags[2])
                .add(R.id.fragment_container, fragments[3], fragmentTags[3])
        transaction!!
                .show(fragments[0])
                .hide(fragments[1])
                .hide(fragments[2])
                .hide(fragments[3])
        transaction!!.commit()
        currentFragment = fragments[0]
    }

    /**
     * @param position
     */
    private fun selectTab(position: Int) {
        when (position) {
            0 -> switchFragment(if (fragments[0] == null) ARouter.getInstance().build("/transaction/main").navigation() as Fragment else fragments[0], 0)
            1 -> switchFragment(if (fragments[1] == null) ARouter.getInstance().build("/quicktrade/main").navigation() as Fragment else fragments[1], 1)
            2 -> information()
            3 -> switchFragment(if (fragments[3] == null) ARouter.getInstance().build("/businesshub/main").navigation() as Fragment else fragments[3], 3)
            else -> {
            }
        }
    }

    private fun information() {
        var openAnimation = Rotate3dAnimation(this@MainActivity,
                0F, 90F, (container.width / 2.0).toFloat(), (container.height / 2.0).toFloat(), (container.width / 2.0).toFloat(), true)
        openAnimation.duration = 500
        openAnimation.fillAfter = true
        openAnimation.interpolator = AccelerateInterpolator()
        openAnimation.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationRepeat(animation: Animation?) {

            }

            override fun onAnimationEnd(animation: Animation?) {
                ARouter.getInstance().build("/information/main/activity").navigation(this@MainActivity, 5)
            }

            override fun onAnimationStart(animation: Animation?) {
//                ARouter.getInstance().build("/information/main/activity").navigation(this@MainActivity, 5)
            }

        })
        container.startAnimation(openAnimation)

    }

    /**
     * 切换Fragment
     *
     * @param to
     * @param position
     */

    private fun switchFragment(to: Fragment, position: Int) {
        if (currentFragment !== to) {
            transaction = fm!!.beginTransaction()
            // 先判断是否被add过
            if (!to.isAdded) {
                // 隐藏当前的fragment，add下一个到Activity中
                transaction!!.hide(currentFragment)
                        .add(R.id.fragment_container, to, fragmentTags[position]).commitAllowingStateLoss()
            } else {
                // 隐藏当前的fragment，显示下一个
                transaction!!.hide(currentFragment).show(to).commitAllowingStateLoss()
            }
            currentFragment = to
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode==6) {

        }
        bottom_view.selectTab(0)
        backAnimation()
    }

    private fun backAnimation() {
        var openAnimation = Rotate3dAnimation(this@MainActivity,
                270F, 360F, (container.width / 2.0).toFloat(), (container.height / 2.0).toFloat(), (container.width / 2.0).toFloat(), false)
        openAnimation.duration = 500
        openAnimation.fillAfter = true
        openAnimation.interpolator = AccelerateInterpolator()
        openAnimation.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationRepeat(animation: Animation?) {

            }

            override fun onAnimationEnd(animation: Animation?) {

            }

            override fun onAnimationStart(animation: Animation?) {
            }

        })

        container.startAnimation(openAnimation)
    }
}
